﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using We.Moonlight.MSql;
using We.Moonlight.Geometry;

namespace We.Moonlight.ViItem
{
    public abstract class IVi
    {
        
        public static IVi NewIVi(string FileName, int Desk, int Layer, string Name, ViType VT)
        {
            SqlViItem.Insert(Desk, 100, 100, 0.0d, 1.0d, false, Name, Layer, VT, FileName);
            return SqlViItem.SelectLast();
        }
        public static Bitmap getPreview(string FN)
        {
            string FExt = FN.Split("."[0]).Last();
            KeyValueQuadruplet<string, ShellType, string, FilePreviewMethod> F = FileAssoc.RetrieveValue(FExt);
            switch (F.Value3)
            {
                case FilePreviewMethod.Bitmap:
                    try
                    {

                        return (Bitmap)Image.FromFile(FN);
                    }
                    catch (FileNotFoundException FNFX)
                    {
                        throw new FileNotFoundException("File \'" + FN + "\' could not be found.", FNFX);
                    }
                    catch (Exception X)
                    {
                        throw X;
                    }
                case FilePreviewMethod.Icon:
                    return new Icon(FN, 256, 256).ToBitmap();
                case FilePreviewMethod.PreviewHandler:
                    return global::We.Moonlight.Properties.Resources.EmptyViDoc;
                case FilePreviewMethod.Text:
                    return global::We.Moonlight.Properties.Resources.EmptyViDoc;
                default:
                    return global::We.Moonlight.Properties.Resources.EmptyViDoc;
            }
        }
        public static ViFileType getType(string FN)
        {
            string FExt = FN.Split("."[0]).Last();
            KeyValueQuadruplet<string, ShellType, string, FilePreviewMethod> F = FileAssoc.RetrieveValue(FExt);
            switch (F.Value3)
            {
                case FilePreviewMethod.Bitmap:
                    return ViFileType.Image;
                default:
                    return ViFileType.Document;
            }
        }
        public static Bitmap getText(string lines, int width, int height, Font F)
        {
            return getLines(lines, new Size(width, height), Color.White, F, 12, 5, 12);
        }
        public static Bitmap getLines(string lines, Size Sz, Color col, Font F, int TopMargin, int LineMargin, int SideMargin)
        {

            Bitmap BMP = new Bitmap(Sz.Width, Sz.Height);
            Graphics g = Graphics.FromImage(BMP);
            g.Clear(Color.Beige);
            for (
                 int i = TopMargin + (int)(F.Size);
                 i < BMP.Height;
                 i += LineMargin + (int)(F.Size) + 5
                 )
            {
                g.DrawLine(Pens.DimGray, new Point(SideMargin, i), new Point(BMP.Width - SideMargin, i));
            }

            g.DrawString(lines, F, Brushes.Black, new Point(SideMargin, TopMargin));

            return BMP;
        }

        public abstract int ID { get; }
        public abstract string Name { get; set; }
        public abstract int DeskID { get; set; }
        public abstract int Layer { get; set; }

        public abstract Bitmap Bmp { get; }
        public abstract Point[] Corners { get; }

        public abstract void Translate(int x, int y, bool abs);
        public abstract void Rotate(double angle, bool abs);
        public abstract void Scale(double factor, bool abs);

        public abstract Point Location { get; set; }
        public abstract double Rotation { get; set; }
        public abstract double ScaleFactor { get; set; }
        public abstract bool Flipped { get; set; }

        public abstract void Flip();
        public abstract void Flip(FlipSide FS);
        public abstract void Flip(bool ShowNotes);
        public abstract void Flash(bool flashing);

        public void Draw(ref Graphics g)
        {
            g.DrawImage(Bmp, Corners);
        }

        public abstract void Clicked(MouseButtons MB);

        public abstract bool isOn(Point Pt);

        public abstract void Insert();
        public abstract void Update();
        public abstract void Delete();

        public abstract ViType Type { get; }
        public abstract ViFileType FileType { get; }

        public abstract FileInfo File { get; set; }
    }
}
